import {useMemo} from 'react'
import {useSearchParams, URLSearchParamsInit} from 'react-router-dom'
import {cleanObject} from 'utils'

/**
 * 返回页面url中,指定键的参数值
 * */
export const useUrlQueryParam = <K extends string>(keys: K[]) => {
    const [searchParams, setSearchParams] = useSearchParams()
    return [
        useMemo(
            () => keys.reduce((prev, key) => {
                return {...prev, [key]: searchParams.get(key) || ''}
            }, {} as { [key in K]: string }),
            // eslint-disable-next-line react-hooks/exhaustive-deps
            [searchParams]
        ),
        (params: Partial<{ [key in K]: unknown }>) => {
            //iterator
            const o = cleanObject({...Object.fromEntries(searchParams), ...params}) as URLSearchParamsInit
            return setSearchParams(o)
        }
        // searchParams
    ] as const
    // console.log(searchParams.get('name'))
}
// const a = ['jack', 12, {gender: 'male'}] as const
